﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using RedHouse.Data.Entities;
using RedHouse.Data.Common;
using RedHouse.Data;
using System.Globalization;

namespace RedHouse.AdminControl.UserControls
{
    public partial class UCNewsDetail : System.Web.UI.UserControl
    {
        public string Message { get; set; }
        private ILocationService _locationService;
        private ILanguageService _languageService;
        private ICategoryService _categoryService;
        private INewsService _newsService;

        protected void Page_Load(object sender, EventArgs e)
        {
            _languageService = new LanguageService();
            _locationService = new LocationService();
            _categoryService = new CategoryService();
            _newsService = new NewsService();


            if (!IsPostBack)
            {
                Initialize();
            }

            btnSave.Click += new EventHandler(btnSave_Click);
        }


        void btnSave_Click(object sender, EventArgs e)
        {
            RedHouseNews news = GetNewsFromView();
            OperationResult result = _newsService.SaveNews(news);
            ErrorPanel.Visible = !result.Result;
            SuccessPanel.Visible = result.Result;
        }

        private RedHouseNews GetNewsFromView()
        {
            DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
            dtfi.ShortDatePattern = "MM/dd/yyyy";
            dtfi.DateSeparator = "/";
            RedHouseNews news = new RedHouseNews()
            {
                Id = Guid.NewGuid(),
                NewsGroup = RedHouseGroup.GroupList[0].Value,
                StartDate = !string.IsNullOrEmpty(hdfStartDate.Value)?DateTime.Parse(hdfStartDate.Value, dtfi):DateTime.Now,
                EndDate = !string.IsNullOrEmpty(hdfEndDate.Value)?DateTime.Parse(hdfEndDate.Value, dtfi):DateTime.Now,
                CreatedBy = "admin",
                CreatedDate = DateTime.Now,
                CategoryId = new Guid(cboCategory.SelectedValue),
                LocationId = int.Parse(cboLocation.SelectedValue)
            };

            Detail detail = new Detail()
            {
                ID = Guid.NewGuid(),
                AssociatedID = news.Id,
                LangID = new Guid(cboLanguage.SelectedValue),
                Description = hdfDesc.Value,
                GroupType = RedHouseGroup.GroupList[0].Value,
                ImageUrl = txtImageUrl.Text,
                Title = txtName.Text,
                CreatedDate = DateTime.Now,
                CreatedBy = "admin",                
            };

            news.NewsDetail = detail;

            return news;
        }


        private void Initialize()
        {
            Message = string.Empty;
            ErrorPanel.Visible = false;
            SuccessPanel.Visible = false;

            //Databinding
            cboLocation.DataSource = _locationService.GetAllLocation();
            cboLocation.DataTextField = "Name";
            cboLocation.DataValueField = "ID";
            cboLocation.DataBind();

            cboLanguage.DataSource = _languageService.GetAllLanguage();
            cboLanguage.DataTextField = "Description";
            cboLanguage.DataValueField = "ID";
            cboLanguage.DataBind();

            cboCategory.DataSource = _categoryService.GetAll().Where(c=>c.Group==RedHouseGroup.GroupList[0].Value).ToList();
            cboCategory.DataTextField = "Title";
            cboCategory.DataValueField = "Id";
            cboCategory.DataBind();
        }
    }
}